//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.menggudf.http;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpsCertificates {
    private static Log logger = LogFactory.getLog(HttpsCertificates.class);

    public HttpsCertificates() {
    }

    public static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init((KeyManager[])null, trustAllCerts, (SecureRandom)null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        String oldProtocol = System.getProperty("https.protocols");
        logger.debug("获取https.protocols：" + oldProtocol);
        if (StringUtils.isEmpty(oldProtocol)) {
            SSLSocket socket = (SSLSocket)sc.getSocketFactory().createSocket();
            String[] supportedProtocols = socket.getSupportedProtocols();
            logger.debug("Supported Protocols(" + supportedProtocols.length + "): " + ArrayUtils.toString(supportedProtocols));
            String[] enabledProtocols = socket.getEnabledProtocols();
            logger.debug("Enabled Protocols(" + enabledProtocols.length + "): " + ArrayUtils.toString(enabledProtocols));
            StringBuffer protocols = new StringBuffer();
            String[] var11 = supportedProtocols;
            int var10 = supportedProtocols.length;

            String supportedProtocol;
            for(int var9 = 0; var9 < var10; ++var9) {
                supportedProtocol = var11[var9];
                if (supportedProtocol.indexOf("TLSv") != -1) {
                    protocols.append(",");
                    protocols.append(supportedProtocol);
                }
            }

            supportedProtocol = protocols.toString().replaceFirst(",", "");
            logger.info("设置https.protocols：" + supportedProtocol);
            System.setProperty("https.protocols", supportedProtocol);
        }

    }

    static class miTM implements TrustManager, X509TrustManager {
        miTM() {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }
    }
}
